/*
 * Created by JFormDesigner on Tue Jul 02 10:41:41 CST 2024
 */

package scct.zhang.ui;

import scct.zhang.dao.GroupDao;
import scct.zhang.model.Group;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 * @author lenovo
 */
public class PanelGroup extends JPanel {
    DefaultTableModel Searchmodel = null;
    FormMain main;
    DefaultTableModel model;
    public PanelGroup(FormMain main) {
        this.main=main;
        initComponents();
        initView();
    }

    private void initView() {
        model=(DefaultTableModel) tabGroup.getModel();
        model.addColumn("id");
        model.addColumn("群组名");
        model.addColumn("创建者");
        model.addColumn("创建时间");
        getTable();
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        dc.setHorizontalAlignment(SwingConstants.CENTER);
        tabGroup.setDefaultRenderer(Object.class,dc);
    }

    private void getTable() {
        model.setRowCount(0);
        GroupDao groupDao=new GroupDao();
        List<Group> allGroup=groupDao.getAllGroup();
        for (Group group:allGroup){
            model.addRow(new Object[]{group.getId(),group.getGroupname(),group.getCreatorname(),group.getCreatetime()});
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgGroupAdd dlgGroupAdd=new DlgGroupAdd(main);
        dlgGroupAdd.setModal(true);
        dlgGroupAdd.setVisible(true);
        getTable();
    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        if (tabGroup.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要修改的群组");
            return;
        }
        int index = tabGroup.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgGroupUpdate dlgGroupUpdate=new DlgGroupUpdate(main,id);
        dlgGroupUpdate.setModal(true);
        dlgGroupUpdate.setVisible(true);
        getTable();
    }

    private void btnDelete(ActionEvent e) {
        // TODO add your code here
        if (tabGroup.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要删除的群组");
            return;
        }
        int index = tabGroup.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgGroupDelete dlgGroupDelete=new DlgGroupDelete(main,id);
        dlgGroupDelete.setModal(true);
        dlgGroupDelete.setVisible(true);
        getTable();
    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here

//        System.out.println(e.getSource());
        tabGroup.setModel(model);
        Searchmodel = new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("群组名");
        Searchmodel.addColumn("创建者");
        Searchmodel.addColumn("创建时间");
        Searchmodel.setRowCount(0);
        String searchkey = txtSearch.getText();
//        System.out.println(searchkey);
        String searchList = (String) comSearch.getSelectedItem();
//        System.out.println(searchList);
        int row = tabGroup.getRowCount();
//        System.out.println("row:" + row);
        int column = tabGroup.getColumnCount();
//        System.out.println("column:" + column);
        for (int i = 0; i < row; i++) {
            if (tabGroup.getRowCount() < row){
            }else {
                for (int j = 0; j < column; j++) {

                    if (Objects.equals(searchList, tabGroup.getColumnName(j))) {
//                        System.out.println("单元格的值：" + tabGroup.getValueAt(i, j));
                        String cellValue = tabGroup.getValueAt(i, j).toString().toLowerCase();
//                        System.out.println("cellValue:" + cellValue);
                        if (cellValue.contains(searchkey)) {
                            Object[] rowDate = new Object[tabGroup.getColumnCount()];
                            for (int k = 0; k < tabGroup.getColumnCount(); k++) {
                                rowDate[k] = tabGroup.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabGroup.setRowSelectionAllowed(true);
                            break;
                        }

                    }
                }
            }
        }
        tabGroup.setModel(Searchmodel);
    }
    


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        panel1 = new JPanel();
        comSearch = new JComboBox<>();
        txtSearch = new JTextField();
        btnSearch = new JButton();
        btnDelete = new JButton();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabGroup = new JTable();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing.border
        .EmptyBorder(0,0,0,0), "JFor\u006dDesi\u0067ner \u0045valu\u0061tion",javax.swing.border.TitledBorder.CENTER,javax
        .swing.border.TitledBorder.BOTTOM,new java.awt.Font("Dia\u006cog",java.awt.Font.BOLD,
        12),java.awt.Color.red), getBorder())); addPropertyChangeListener(new java.beans
        .PropertyChangeListener(){@Override public void propertyChange(java.beans.PropertyChangeEvent e){if("bord\u0065r".equals(e.
        getPropertyName()))throw new RuntimeException();}});
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- comSearch ----
            comSearch.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u7fa4\u7ec4\u540d",
                "\u521b\u5efa\u8005",
                "\u521b\u5efa\u65f6\u95f4"
            }));
            panel1.add(comSearch);
            comSearch.setBounds(new Rectangle(new Point(10, 5), comSearch.getPreferredSize()));
            panel1.add(txtSearch);
            txtSearch.setBounds(120, 5, 95, txtSearch.getPreferredSize().height);

            //---- btnSearch ----
            btnSearch.setText("\u641c\u7d22");
            btnSearch.addActionListener(e -> btnSearch(e));
            panel1.add(btnSearch);
            btnSearch.setBounds(new Rectangle(new Point(220, 5), btnSearch.getPreferredSize()));

            //---- btnDelete ----
            btnDelete.setText("\u5220\u9664");
            btnDelete.addActionListener(e -> btnDelete(e));
            panel1.add(btnDelete);
            btnDelete.setBounds(new Rectangle(new Point(346, 5), btnDelete.getPreferredSize()));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);
            btnAdd.setBounds(new Rectangle(new Point(429, 5), btnAdd.getPreferredSize()));

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);
            btnUpdate.setBounds(new Rectangle(new Point(512, 5), btnUpdate.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabGroup);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JPanel panel1;
    private JComboBox<String> comSearch;
    private JTextField txtSearch;
    private JButton btnSearch;
    private JButton btnDelete;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabGroup;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
